<?php
require_once 'CRUD_interface';

class Tasks_controller extends CI_Controller implements CRUD_interface{
	
	public function __construct(){
		parent::__construct();
        $this->load->model('task');
	}
	
	//-------------------------------------------------------------------------
	//	VIEWS METHODS
	//-------------------------------------------------------------------------
	
	/**
	 * Displays create task view
	 */
	public function create_view(){
		$data['general'] = array('priority_options' => array('1' => '1', '2' => '2', '3' => '3'),
										 'status_options' 	=> array('0' => '0', '1' => '1', '2' => '2'),
										 'completed_options'=> array('0' => '0', '1' => '1', '2' => '2')
										);
										
		$this->load->view('task_upsert', $data);
	}
   	
	/**
	 * Displays edit task view
	 * @param $id - task id
	 */
	public function edit_view($id = NULL){
		$data['general'] = array('priority_options' => array('1' => '1', '2' => '2', '3' => '3'),
								 'status_options' 	=> array('0' => '0', '1' => '1', '2' => '2'),
								 'completed_options'=> array('0' => '0', '1' => '1', '2' => '2')
								);
		if($id != NULL && $id != ''){
			$task = $this->task->get_task($id);
			$data['task'] = $task[0];
		}
		
		$this->load->view('task_upsert', $data);
	}
	
	/**
	 * Displays task detail view
	 * @param $id - task id
	 */
   	public function detail_view($id = NULL){
   	
   	}
	
   	/**
   	 * Displays tasks list view
   	 */
   	public function list_view(){
   		$this->load->view('tasks_list');
   	}

   	//-------------------------------------------------------------------------
	//	CRUD METHODS
	//-------------------------------------------------------------------------
   	
   	/**
   	 * Gets a task from database
   	 * @param $id - task id
   	 * @see CRUD_interface::get()
   	 */
   	public function get($id = NULL){
   	
   	}
   	
   	/**
   	 * Saves a task into the database
   	 * @see CRUD_interface::save()
   	 */
   	public function save(){
   		$id						= $this->input->post('id');
		$subject 				= $this->input->post('subject');
		$description 			= $this->input->post('description');
		$priority 				= $this->input->post('priority');
		$status 				= $this->input->post('status');
		$estimated_hours 		= $this->input->post('estimated_hours');
		$worked_hours 			= $this->input->post('worked_hours');
		$percentaje_completed 	= $this->input->post('percentaje_completed');
		
		
		if($id != NULL && $id != ''){
			$this->task->update_task($id, $subject, $description, $priority, $status, $estimated_hours, $worked_hours, $percentaje_completed);
		}
		else{
			$insertId = $this->task->create_task($subject, $description, $priority, $status, $estimated_hours, $worked_hours, $percentaje_completed);
		}
		
		redirect('/index.php/tasks', 'refresh');
   	}
   	
   	/**
   	 * Removes a task from database
   	 * @param $id - task id
   	 * @see CRUD_interface::remove()
   	 */
   	public function remove($id = NULL){
   		$this->task->delete_task($id);
		redirect('/index.php/tasks', 'refresh');
   	}
   	
   	
   	/**
   	 * Gets tasks list for jqgrid
   	 */
   	public function get_json_tasks_list() {
   		$page 			= $this->input->post('page');
		$limit 			= $this->input->post('rows');
		$order_by_field = $this->input->post('sidx');
		$order_by_type 	= $this->input->post('sord');
		
		if(!$order_by_field) $sidx =1;
		
		$count = $this->task->get_tasks_count();

		if( $count > 0 ) {
			$total_pages = ceil($count/$limit);
		} else {
			$total_pages = 0;
		}
		if ($page > $total_pages) $page=$total_pages;
		$start = $limit*$page - $limit;
		
		$responce->page 	= $page;
		$responce->total 	= $total_pages;
		$responce->records 	= $count;
		
		$tasks = $this->task->get_tasks($order_by_field, $order_by_type, $start , $limit); 
		
		for($i=0; $i< sizeof($tasks); $i++) {
			$responce->rows[$i]['id'] = $tasks[$i]['id'];
			$responce->rows[$i]['cell'] = array($tasks[$i]['id'], 
												$tasks[$i]['subject'], 
												$tasks[$i]['priority'],
												$tasks[$i]['creation_date'],
												$tasks[$i]['status'],
												$tasks[$i]['estimated_hours'], 
												$tasks[$i]['worked_hours'], 
												$tasks[$i]['percentaje_completed'], 'action');
		}
     	
		echo json_encode($responce);
   	}
   	
   	
}